//
//  ChatFaceContentView.m
//  ChangChat
//
//  Created by Cocoa on 2019/8/26.
//  Copyright © 2019 yibai. All rights reserved.
//

#import "ChatFaceContentView.h"

@interface ChatFaceContentView ()

@property (nonatomic, strong) UIButton *deleteBtn;
@property (nonatomic, strong) NSMutableArray *faceViewArr;

@end

@implementation ChatFaceContentView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}
- (void)setupUI {
    self.deleteBtn = UIButton.k_initType(UIButtonTypeCustom).k_img(UIImage.k_imgName(@"DeleteEmoticonBtn"));
    self.deleteBtn.tag = -1;
    [self.deleteBtn addTarget:self action:@selector(didClickFaceItemBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.deleteBtn];
    self.faceViewArr = [NSMutableArray array];
}

- (void)showFaceGroup:(NSMutableArray *)faceGroup fromIndex:(NSInteger)fromIndex count:(NSInteger)count {
    
    
    int index = 0;
    float spaceX = 12;
    float spaceY = 10;
    int row = 3;
    int col = 7;
    float w = (ScreenWidth - spaceX * 2) / col;
    float h = (self.height - spaceY * (row-1)) / row;
    float x = spaceX;
    float y = spaceY;
    
    UIView *lastBtn = nil;
    for (NSInteger i = fromIndex; i < fromIndex + count; i++) {
        
//        NSMutableArray *btsArr = [NSMutableArray array];
        UIButton *btn = nil;
        if (index < self.faceViewArr.count) {
            btn = [self.faceViewArr objectAtIndex:index];
        } else {
            btn = UIButton.k_initType(UIButtonTypeCustom);
            [btn addTarget:self action:@selector(didClickFaceItemBtnAction:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:btn];
            [self.faceViewArr addObject:btn];
            if (index == 0 || (index)%col == 0) {
                [btn mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.mas_offset(spaceX);
                    if (lastBtn == nil) {
                        make.top.mas_offset(spaceY);
                    } else {
                        make.top.equalTo(lastBtn.mas_bottom);
                    }
                    make.size.mas_equalTo(CGSizeMake(w, h));
                }];
            } else {
                [btn mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(lastBtn.mas_right);
                    make.top.equalTo(lastBtn);
                    make.size.mas_equalTo(CGSizeMake(w, h));
                }];
            }
        }
        
        index++;
        lastBtn = btn;
        
        if (i >= faceGroup.count || i < 0) {
            btn.hidden = YES;
        } else {
            ChatFaceItem *face = [faceGroup objectAtIndex:i];
            btn.tag = i;
            btn.hidden = NO;
            btn.k_img(UIImage.k_imgName(face.face_name)).k_frame(CGRectMake(x, y, w, h));
            
            
        }
    }
    
    self.deleteBtn.hidden = NO;
    self.deleteBtn.frame = CGRectMake(x, y, w, h);
    if (lastBtn) {
        
        [self.deleteBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(lastBtn);
            make.left.equalTo(lastBtn.mas_right);
            make.size.mas_equalTo(CGSizeMake(w, h));
        }];
    }
}

- (void)didClickFaceItemBtnAction:(UIButton *)sender {
    if (self.ClickFaceItemViewAction) {
        self.ClickFaceItemViewAction(sender);
    }
}

@end
